package org.huangrui.javase.thread;

import org.junit.Test;

/**
 * @Author hr
 * @Create 2025-11-08 13:09
 **/
public class T01DaemonThread {
    /**
     * 测试守护线程
     * @param args
     */
    public static void main(String[] args) {
        MyDaemon m = new MyDaemon();
        // m.setDaemon(true);
        m.start();

        for (int i = 1; i <= 100; i++) {
            System.out.println("main:" + i);
        }
    }

    /**
     * 不要在junit中测试多线程
     * @throws Exception
     */
    @Test
    public void test1()throws Exception{
        new Thread(){
            @Override
            public void run() {
                while (true) {
                    System.out.println("我想要永生....");
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();

        for (int i = 1; i <= 10; i++) {
            System.out.println("junit:" + i);
        }
    }
}

class MyDaemon extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("我一直守护者你...");
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
